SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO


/** Create Stored Functions **/

CREATE FUNCTION dbo.[GetCAPName]
(
	@PWAP_location VARCHAR(255)
)
RETURNS VARCHAR(100)
AS
BEGIN
	DECLARE @URL NVARCHAR(MAX)
	DECLARE @INDEX INT
	DECLARE @CAPNAME VARCHAR(100)
	DECLARE @USEHTTPS BIT
	
	SET @INDEX = 0
	SET @URL = ''
	SET @CAPNAME = ''
	SET @USEHTTPS = 0
	
	SET @URL = @PWAP_location
	SET @INDEX = CHARINDEX('https://', @URL)

	IF (@INDEX > 0) SET @USEHTTPS = 1

	IF (@USEHTTPS = 1) SET @URL = REPLACE(@URL, 'https://', '')
	ELSE SET @URL = REPLACE(@URL, 'http://', '')

	SET @INDEX = CHARINDEX('.', @URL)

	SET @CAPNAME = SUBSTRING(@URL, 0, @INDEX)

	RETURN	@CAPNAME
END

GO
